import { Component, PropsWithChildren } from 'react'
import { Provider } from 'mobx-react'

import counterStore from './store/counter'
import './custom-theme.global.scss'
import { userStore, recordStore } from './store';
import './app.global.less'
import Taro from '@tarojs/taro';

const stores = {
  userStore,
  recordStore
}

class App extends Component<PropsWithChildren>  {
  componentDidMount() { }

  componentDidShow() { }

  componentDidHide() { }

  onLaunch() {

    const token = Taro.getStorageSync('token');
    if (token) {
      userStore.requestUserInfo();
    } else {
      Taro.showModal({
        title: '提示',
        content: '请先登录',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // 用户点击确定后跳转到登录页面
            Taro.redirectTo({
              url: '/pages/login/index'
            })
          }
        }
      })
    }
  }

  // this.props.children 就是要渲染的页面
  render() {
    return (
      <Provider {...stores}>
        {this.props.children}
      </Provider>
    )
  }
}

export default App
